 #include <reg51.h>  	
#include"lcd.h"
//int i,j;
sbit rel1=P2^0;
sbit rel2=P2^1;
sbit rel3=P2^2;

sbit csw=P2^3;
unsigned char a[4][3]={'1','2','3','4',
							  '5','6','7','8',
							  '9','0','a','b'};
unsigned char val[] = {'0','1','2','3','4','5','6','7','8','9','+','-'};
unsigned char r=0,c=0,ch=0;
unsigned int ret=0;

void tx(unsigned char a)
{
      	SBUF = a;
		 	while(TI == 0);
			TI = 0;
}
			
void call_end()
 {
 	tx('a');
	tx('t');
	tx('h');
   tx(0x0d);
 }
 

//*********************************************************************************
//                SERIAL INIT_TX_RX
//*********************************************************************************
 
void baud_init ()
	{	
		TMOD = 0x20;
		TH1 = 0xFD;
		SCON = 0x50;
		TR1 = 1;
	}
	

//*******************************************************************************

unsigned char kb()
  {
  unsigned char li=0,lj=0xee,lk,lr=45;
  for(li=0;li<4;li++)
  {
  P1=lj;
  lk=P3;
     LCD_delay(15);
  lk &= 0x38; 
  if(lk!=0x38)
  {
  if(lk==0x30)
  lr=0+(li*3);
  if(lk==0x28)
  lr=1+(li*3);
  if(lk==0x18)
  lr=2+(li*3);
 }
  lj = (lj<<1)+1;
  }
  return(lr);
    }
    

//********************************************************************************

unsigned char eid()
{
	unsigned char i=0, flag=1;
		LCD_row2();
//	for(i=0;i<4;i++)
//		{
		while(1)
		{
      ch=kb(); 		 	
      if(ch!=45 && ch!='+' && ch!='-')
      	{
      	LCD_putc(val[ch]); 
      	LCD_delay(5000);
      	LCD_delay(5000);
      	break;
      	}
  //    }
       
      }
      return(ch);
}


//********************************************************************************

unsigned char passwd(unsigned char j)
{
	unsigned char i=0, flag=1;
		LCD_row2();
	for(i=0;i<4;i++)
		{
		while(1)
		{
      ch=kb(); 		 	
      if(ch!=45 && ch!='+' && ch!='-')
      	{
      	LCD_putc(val[ch]);
      	if(a[i+j]==val[ch])
      	{
      		flag=0;
      	} 
      	LCD_delay(5000);
      	LCD_delay(5000);
      	LCD_command((0xC0)+i);
      	LCD_putc('*');
      	break;
      	}
      } 
      }
}
	
//*********************************************************************
	
void main( void )
{              
	LCD_init();
  	baud_init ();
  	LCD_row1();
  	LCD_puts("Enter EMP ID:");
  	P2=0;
  	while(1)
  	{
  	     sw=eid(); 		 	
   if(sw<3)
   {
   test=passwd(sw)
   	if(test==0)
   	{
  			LCD_clear();
  			LCD_row1();
  			LCD_puts("1.ON-BAR"); 
  			LCD_row2();
  			LCD_puts("2.TRIPPED");  
  			LCD_delay(5000); 
  			
  			ch=45;
				while((ch!=1)||(ch!=2))  
   			{ch=kb();}
   			
   		if(ch==1)
   		{
   		onbar();
   		}
   		else if(ch==2)
   		{
   		trip(sw);
   		}   	
   	}
   	else
   	{
  			LCD_clear();
  			LCD_row1();
  			LCD_puts("Enter Valid PWD");   
  			LCD_delay(5000);  		
  			LCD_delay(5000);   			
  			LCD_clear();
  			LCD_row1();  
  			LCD_puts("Enter EMP ID:");   	
   	}
   }
   
   else
   {
  	LCD_clear();
  	LCD_row1();
  	LCD_puts("Enter Valid ID");   
  	LCD_delay(5000);  		
  	LCD_delay(5000);  
  	LCD_clear();
  	LCD_row1(); 
  	LCD_puts("Enter EMP ID:");
  	}  	
  	}   
}


